﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlDangKyTuVanVien : System.Web.UI.UserControl
{
    
    BCDataCenter.DataAccessLayer.DAL dt;
    MethodClassCommon dt1;
    DataTable tinh;
    DataSet ds,ds2,ds3;
    string error="";
   
    static bool flag = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
                flag = false;
                HienThiTinh();
                HienThiQuan();
                NoiCap();
                trinhdo();
                txtSearch.Text = "";
            
        }
    }
    void NoiCap()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        tinh = dt.get_data_table("GET_DATA_PROVINCE");
        DropNoiCap.DataSource = tinh;
        DropNoiCap.DataTextField = "ProvinceName";
        DropNoiCap.DataValueField = "ProvinceCode";
        DropNoiCap.DataBind();
       
    }
    void trinhdo()
    {
        for (int i = 1; i < 13; i++)
        {
            DropTrinhDo.Items.Add(Convert.ToString(i) + "/12");
        }
        DropTrinhDo.SelectedIndex = DropTrinhDo.Items.Count - 1;
    }
    void HienThiTinh()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        tinh=dt.get_data_table("GET_DATA_PROVINCE");
        DropTinh.DataSource = tinh;
        DropTinh.DataTextField = "ProvinceName";
        DropTinh.DataValueField = "ProvinceCode";
        DropTinh.DataBind();
    }
    void HienThiQuan()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        ds = dt.MyExecuteQuery(ref error, "GET_DATA_DISTRICTOWN_PROVINCECODE", CommandType.StoredProcedure, new SqlParameter("@ProvinceCode", DropTinh.SelectedValue.ToString()));
        DropQuanHuyen.DataSource = ds.Tables[0];
        DropQuanHuyen.DataTextField = "TownName";
        DropQuanHuyen.DataValueField = "TownCode";
        DropQuanHuyen.DataBind();
    }
    void clear()
    {
        txtMaNguoiGioiThieu.Text = "";
        txtTenTuVanVien.Text = "";
        txtMaNguoiGioiThieu.Text = "";
        txtSoCMND.Text = "";
        txtNgayCap.Text = "";
        txtNgaySinh.Text = "";
        txtSoNha.Text = "";
        txtTenDuong.Text = "";
        txtDtCoDinh.Text = "";
        txtDTDiDong.Text = "";
        txtEmail.Text = "";
        txtNgheNghiep.Text = "";
        txtTenNguoiGioiThieu.Text = "";
        txtNgayDangKy.Text = "";
        CheckDaNopTien.Checked = false;
        CheckDoTuVanVien.Checked = false;
        CheckGioiTinh.Checked = false;
       
       
    }  
    void grid()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        dt1 = new MethodClassCommon();
        try
        {
            ds2 = dt.MyExecuteQuery(ref error, "SEARCH_ARCOMPETITOR", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearch.Text), new SqlParameter("@sitecode", Convert.ToString(Session["Sitecode"])));
            if (ds2.Tables[0].Rows.Count == 1)
            {
                
                ds3 = dt.MyExecuteQuery(ref error, "GET_DATA_ARCOMPETITOR_salesp_key", CommandType.StoredProcedure, new SqlParameter("@salesp_key", ds2.Tables[0].Rows[0][0].ToString()));
                try
                {
                    DropNoiCap.SelectedValue = ds3.Tables[0].Rows[0][6].ToString();
                }
                catch
                {
                    DropNoiCap.SelectedIndex = -1;
                }

                try
                {
                    DropTinh.SelectedValue = ds3.Tables[0].Rows[0][9].ToString();
                }
                catch
                {
                    DropTinh.SelectedIndex = -1;
                }
                ds = dt.MyExecuteQuery(ref error, "GET_DATA_DISTRICTOWN_PROVINCECODE", CommandType.StoredProcedure, new SqlParameter("@ProvinceCode", DropTinh.SelectedValue.ToString()));
                DropQuanHuyen.DataSource = ds.Tables[0];
                DropQuanHuyen.DataTextField = "TownName";
                DropQuanHuyen.DataValueField = "TownCode";
                DropQuanHuyen.DataBind();
                try
                {
                    DropQuanHuyen.SelectedValue = ds3.Tables[0].Rows[0][10].ToString();

                }
                catch
                {
                    DropQuanHuyen.SelectedIndex = -1;
                }
                try
                {
                    DropTrinhDo.SelectedValue = ds3.Tables[0].Rows[0][13].ToString();
                }
                catch
                {
                    DropTrinhDo.SelectedIndex = -1;
                }
                finally
                {
                    txtMa.Text = ds3.Tables[0].Rows[0][0].ToString();
                    txtTenTuVanVien.Text = ds3.Tables[0].Rows[0][1].ToString();
                    if (ds3.Tables[0].Rows[0][2].ToString() == "True")
                    {
                        CheckGioiTinh.Checked = true;
                    }
                    else
                    {
                        CheckGioiTinh.Checked = false;
                    }
                    txtMaNguoiGioiThieu.Text = ds3.Tables[0].Rows[0][3].ToString();
                    txtTenNguoiGioiThieu.Text = dt1.GetDataDepend("get_Recommender", "salesp_key", txtMaNguoiGioiThieu.Text.Trim());
                    txtSoCMND.Text = ds3.Tables[0].Rows[0][4].ToString();
                    txtNgayCap.Text = ds3.Tables[0].Rows[0][5].ToString();
                    txtNgaySinh.Text = ds3.Tables[0].Rows[0][7].ToString();
                    txtSoNha.Text = ds3.Tables[0].Rows[0][8].ToString();
                    txtDtCoDinh.Text = ds3.Tables[0].Rows[0][11].ToString();
                    txtDTDiDong.Text = ds3.Tables[0].Rows[0][12].ToString();
                    txtEmail.Text = ds3.Tables[0].Rows[0][14].ToString();
                    txtNgheNghiep.Text = ds3.Tables[0].Rows[0][15].ToString();
                    if (ds3.Tables[0].Rows[0][16].ToString() == "True")
                    {
                        CheckDaNopTien.Checked = true;
                    }
                    else
                    {
                        CheckDaNopTien.Checked = false;
                    }
                    if (ds3.Tables[0].Rows[0][17].ToString() == "True")
                    {
                        CheckDoTuVanVien.Checked = true;
                    }
                    else
                    {
                        CheckDoTuVanVien.Checked = false;
                    }
                    txtNgayDangKy.Text = ds3.Tables[0].Rows[0][18].ToString();
                    txtTenDuong.Text = ds3.Tables[0].Rows[0][19].ToString();
                    TabContainer1.ActiveTabIndex = 0;
                    txtMa.Visible = true;
                    lblTitleMaTvv.Visible = true;
                    flag = false;
                }
            }
            else
            {
                GridView2.DataSource = ds2.Tables[0];
                GridView2.DataBind();
            }
        }
        catch(Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
           
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
        GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
        if (e.CommandName == "chon")
        {
            dt = new BCDataCenter.DataAccessLayer.DAL();
            dt1 = new MethodClassCommon();
             ds3=dt.MyExecuteQuery(ref error,"GET_DATA_ARCOMPETITOR_salesp_key",CommandType.StoredProcedure,new SqlParameter("@salesp_key",grv.Cells[0].Text));
             try
             {
                 DropNoiCap.SelectedValue  = ds3.Tables[0].Rows[0][6].ToString();
             }
             catch
             {
                 DropNoiCap.SelectedIndex = -1;
             }
             try
             {
                 DropTinh.SelectedValue = ds3.Tables[0].Rows[0][9].ToString();
             }
             catch
             {
                 DropTinh.SelectedIndex = -1;
             }
             ds = dt.MyExecuteQuery(ref error, "GET_DATA_DISTRICTOWN_PROVINCECODE", CommandType.StoredProcedure, new SqlParameter("@ProvinceCode", DropTinh.SelectedValue.ToString()));
             DropQuanHuyen.DataSource = ds.Tables[0];
             DropQuanHuyen.DataTextField = "TownName";
             DropQuanHuyen.DataValueField = "TownCode";
             DropQuanHuyen.DataBind();
             try
             {
                 DropQuanHuyen.SelectedValue = ds3.Tables[0].Rows[0][10].ToString();
             }
             catch
             {
                 DropQuanHuyen.SelectedIndex = -1;
             }
             try
             {
                 DropTrinhDo.SelectedValue = ds3.Tables[0].Rows[0][13].ToString();
             }
             catch
             {
                 DropTrinhDo.SelectedIndex = -1;
             }
             finally
             {
                 txtMa.Text = ds3.Tables[0].Rows[0][0].ToString();
                 txtTenTuVanVien.Text = ds3.Tables[0].Rows[0][1].ToString();
               
                 if (ds3.Tables[0].Rows[0][2].ToString() == "True")
                 {
                     CheckGioiTinh.Checked = true;
                 }
                 else
                 {
                     CheckGioiTinh.Checked = false;
                 }
                 txtMaNguoiGioiThieu.Text = ds3.Tables[0].Rows[0][3].ToString();
                 txtTenNguoiGioiThieu.Text = dt1.GetDataDepend("get_Recommender", "salesp_key", txtMaNguoiGioiThieu.Text.Trim());
                 txtSoCMND.Text = ds3.Tables[0].Rows[0][4].ToString();
                 txtNgayCap.Text = ds3.Tables[0].Rows[0][5].ToString();

                 txtNgaySinh.Text = ds3.Tables[0].Rows[0][7].ToString();
                 txtSoNha.Text = ds3.Tables[0].Rows[0][8].ToString();
                 txtDtCoDinh.Text = ds3.Tables[0].Rows[0][11].ToString();
                 txtDTDiDong.Text = ds3.Tables[0].Rows[0][12].ToString();

                 txtEmail.Text = ds3.Tables[0].Rows[0][14].ToString();
                 txtNgheNghiep.Text = ds3.Tables[0].Rows[0][15].ToString();
                 if (ds3.Tables[0].Rows[0][16].ToString()=="True")
                 {
                     CheckDaNopTien.Checked = true;
                 }
                 else
                 {
                     CheckDaNopTien.Checked = false;
                 }
                 if (ds3.Tables[0].Rows[0][17].ToString() == "True")
                 {
                     CheckDoTuVanVien.Checked = true;
                 }
                 else
                 {
                     CheckDoTuVanVien.Checked = false;
                 }
                 txtNgayDangKy.Text = ds3.Tables[0].Rows[0][18].ToString();
                 txtTenDuong.Text = ds3.Tables[0].Rows[0][19].ToString();
                 txtMa.Visible = true;
                 lblTitleMaTvv.Visible = true;
                 GridView2.DataSource = null;
                 GridView2.DataBind();
                 flag = false;
                 TabContainer1.ActiveTabIndex = 0;
             }
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView2.PageIndex = e.NewPageIndex;
            dt = new BCDataCenter.DataAccessLayer.DAL();
            ds2 = dt.MyExecuteQuery(ref error, "SEARCH_ARCOMPETITOR", CommandType.StoredProcedure, new SqlParameter("@parameter", txtSearch.Text), new SqlParameter("@sitecode", Convert.ToString(Session["Sitecode"])));
            GridView2.DataSource = ds2.Tables[0];
            GridView2.DataBind();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ImgNew_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            TabContainer1.ActiveTabIndex = 0;
            lblTitleMaTvv.Visible = false;
            txtMa.Visible = false;
            HienThiQuan();
            HienThiTinh();
            flag = true;
            clear();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
       
        lblTitleMaTvv.Visible = false;
        txtMa.Visible = false;
        dt1 = new MethodClassCommon();
        string trinhdo = DropTrinhDo.SelectedItem.ToString();
        SqlParameter sex;
        SqlParameter isBooked;
        SqlParameter isQualified;
        SqlParameter Recdate;
        SqlParameter salesp_key;
        SqlParameter salesp_name = new SqlParameter("@salesp_name",txtTenTuVanVien.Text);
        SqlParameter Street;
        SqlParameter BelongTo;
        SqlParameter Recommender = new SqlParameter("@Recommender", txtMaNguoiGioiThieu.Text.Trim());
        SqlParameter IDCard  = new SqlParameter("@IDCard", txtSoCMND.Text);
        SqlParameter IDDate = new SqlParameter("@IDDate", txtNgayCap.Text);
        SqlParameter IDWhere = new SqlParameter("@IDWhere", DropNoiCap.SelectedValue.ToString());
        SqlParameter BirthDate= new SqlParameter("@BirthDate", txtNgaySinh.Text);
        SqlParameter Address  = new SqlParameter("@Address", txtSoNha.Text);
        SqlParameter Province = new SqlParameter("@Province", DropTinh.SelectedValue.ToString());
        SqlParameter Town = new SqlParameter("@Town", DropQuanHuyen.SelectedValue.ToString());
        SqlParameter Tel = new SqlParameter("@Tel", txtDtCoDinh.Text);
        SqlParameter Mobile = new SqlParameter("@Mobile", txtDTDiDong.Text);
        SqlParameter EducationLevel = new SqlParameter("@EducationLevel", trinhdo);
        SqlParameter Email = new SqlParameter("@Email", txtEmail.Text);
        SqlParameter Job= new SqlParameter("@Job", txtNgheNghiep.Text);
        bool kq = false;
        if (flag == true)
        {
            dt = new BCDataCenter.DataAccessLayer.DAL();
            string vsalesp_key;
            try
            {
                vsalesp_key = dt1.GetNewKeySale("GET_LAST_salesp_key");
                
            }
            catch
            {
                vsalesp_key = "0000";
            }
            salesp_key = new SqlParameter("@salesp_key", vsalesp_key);
            
            if (CheckGioiTinh.Checked)
            {
                sex = new SqlParameter("@sex", "True");
            }
            else
            {
                sex = new SqlParameter("@sex", "False");
            }
            
            if (CheckDaNopTien.Checked)
            {
                isBooked = new SqlParameter("@isBooked", "True");
            }
            else
            {
                isBooked = new SqlParameter("@isBooked", "False");
            }
            if (CheckDoTuVanVien.Checked)
            {
                isQualified = new SqlParameter("@isQualified", "True");
            }
            else
            {
                isQualified = new SqlParameter("@isQualified", "False");
            }
            if ((txtNgayDangKy.Text == "") || (txtNgayDangKy.Text == null))
            {
                Recdate = new SqlParameter("@Recdate", DateTime.Now.Date.ToString());
            }
            else
            {
                Recdate = new SqlParameter("@Recdate", txtNgayDangKy.Text);
            }
            Street = new SqlParameter("@Street", txtTenDuong.Text);
            BelongTo = new SqlParameter("@BelongTo", Convert.ToString(Session["Sitecode"].ToString()));
            kq = dt.MyExecuteNonQuery("INSERT_ARCOMPETITOR", CommandType.StoredProcedure, ref error, salesp_key, salesp_name, sex, Recommender, IDCard, IDDate, IDWhere, BirthDate, Address, Province, Town, Tel, Mobile, EducationLevel, Email, Job, isBooked, isQualified, Recdate, Street, BelongTo);
            if (kq)
            {
                uscMsgBox1.AddMessage("Thêm mã tư vấn viên " + vsalesp_key + " hoàn thành", uscMsgBox.enmMessageType.Success);
                clear();
                flag = false;
            }
            else
            {
                uscMsgBox1.AddMessage("Thêm mã tư vấn viên không hoàn thành", uscMsgBox.enmMessageType.Error);
            }
            
        }
        else
        {
            int kq1 = 1;
            salesp_key = new SqlParameter("@salesp_key", txtMa.Text);
            salesp_name = new SqlParameter("@salesp_name", txtTenTuVanVien.Text);
            if (CheckGioiTinh.Checked)
            {
                sex = new SqlParameter("@sex", "True");
            }
            else
            {
                sex = new SqlParameter("@sex", "False");
            }
          
            if (CheckDaNopTien.Checked)
            {
                isBooked = new SqlParameter("@isBooked", "True");
            }
            else
            {
                isBooked = new SqlParameter("@isBooked", "False");
            }
            if (CheckDoTuVanVien.Checked)
            {
                isQualified = new SqlParameter("@isQualified", "True");
            }
            else
            {
                isQualified = new SqlParameter("@isQualified", "False");
            }
            if ((txtNgayDangKy.Text == "") || (txtNgayDangKy.Text == null))
            {
                Recdate = new SqlParameter("@Recdate", DateTime.Now.Date.ToString());
            }
            else
            {
                Recdate = new SqlParameter("@Recdate", txtNgayDangKy.Text);
            }
            Street = new SqlParameter("@Street", txtTenDuong.Text);
            BelongTo = new SqlParameter("@BelongTo", Convert.ToString(Session["Sitecode"].ToString()));
            kq1 = dt1.Update_Data("UPDATE_ARCOMPETITOR", CommandType.StoredProcedure, ref error, salesp_key, salesp_name, sex, Recommender, IDCard, IDDate, IDWhere, BirthDate, Address, Province, Town, Tel, Mobile, EducationLevel, Email, Job, isBooked, isQualified, Recdate, Street, BelongTo);
            if (kq1 == 0)
            {
                uscMsgBox1.AddMessage("Cập nhật tư vấn viên " + txtMa.Text + " hoàn thành", uscMsgBox.enmMessageType.Success);
                clear();
            }
            else
            {
                uscMsgBox1.AddMessage("Cập nhật tư vấn viên " + txtMa.Text + " thất bại", uscMsgBox.enmMessageType.Error);
            }
        }
        
    }
    protected void txtMaNguoiGioiThieu_TextChanged(object sender, EventArgs e)
    {
        try
        {
            txtTenNguoiGioiThieu.Text = txtMaNguoiGioiThieu.Text.Substring(txtMaNguoiGioiThieu.Text.IndexOf(Convert.ToChar("|")) + 2);
            txtMaNguoiGioiThieu.Text = txtMaNguoiGioiThieu.Text.Substring(0, txtMaNguoiGioiThieu.Text.IndexOf(Convert.ToChar("|")));
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            grid();
            Label3.Visible = false;
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void TextBox5_TextChanged(object sender, EventArgs e)
    {
        try
        {
            grid();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void DropTinh_SelectedIndexChanged1(object sender, EventArgs e)
    {
        try
        {
            HienThiQuan();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ImgInsert_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
}
